package Txtfiction;


import Txtfiction.common.Request;
import Txtfiction.common.Response;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ServerThread implements Runnable {
    private final Socket s;

    public ServerThread(Socket accept) {
        this.s = accept;
    }

    public static String path = "D:\\76班java workspace\\Y2Java项目\\JavaSenior\\src\\Txtfiction\\user1.xml";
    public static String path1="D:\\76班java workspace\\Y2Java项目\\JavaSenior\\src\\Txtfiction\\fiction1.xml";

    //登录
    public Response login(Request rq) {
        Response resp = new Response();
        resp.setMsg("当前操作:登录用户");
        String name = (String) rq.get("name");
        String pass = (String) rq.get("pass");
        try {
            XMLDecoder xdc=new XMLDecoder(new FileInputStream(path));
            List<User> list=(List<User>) xdc.readObject();
            xdc.close();
            for (User u:list){
                if (name.equals(u.getName()) && pass.equals(u.getPassword())) {
                    resp.setSuccess(true);
                    resp.setMsg("登录成功");
                    break;
                } else {
                    resp.setSuccess(false);
                    resp.setMsg("错误的用户名和密码!");
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resp;
    }

    //注册
    public Response register(Request rq) {
        Response resp = new Response();
        String name = (String) rq.get("name");
        String pass = (String) rq.get("pass");
        String pass2 = (String) rq.get("repeatpass");
        resp.setMsg("当前操作:注册用户");
        try {
            XMLDecoder xdc=new XMLDecoder(new FileInputStream(path));

            List<User> list=(List<User>) xdc.readObject();
            xdc.close();
            for (User u:list){
                if (!pass.equals(pass2)) {
                    resp.setSuccess(false);
                    resp.setMsg("两次密码不一致！");
                    break;
                } else if (pass.length() < 5) {
                    resp.setSuccess(false);
                    resp.setMsg("密码太短！");
                    break;
                } else if (name.equals(u.getName())) {
                    resp.setSuccess(false);
                    resp.setMsg("用户已存在！");
                    break;
                } else {
                    resp.setSuccess(true);
                    resp.setMsg("注册成功");
                    list.add(new User(name,pass,pass2));
                    XMLEncoder xec=  new XMLEncoder(new FileOutputStream(path));
                    xec.writeObject(list);
                    xec.close();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resp;
    }

    //某一个类型的小说列表
    public Response readinfo(Request req){
        Response resp = new Response();
        int choice = (int) req.get("choice");

        try {
            XMLDecoder xdc=new XMLDecoder(new FileInputStream(path1));
            List<FictionList> list=(List<FictionList>) xdc.readObject();
            xdc.close();
            List<FictionList> list2=new ArrayList<>();
            for (FictionList f:list){
                if (f.getType()==choice){
                    list2.add(f);
                }
            }
            resp.setSuccess(true);
            resp.put("novels",list2);
        }catch (Exception e){
            e.printStackTrace();
            resp.setSuccess(false);
            resp.setMsg(e.getMessage());
        }
        return resp;
    }


    //读武侠
    public Response redwuxia(Request req){
        Response resp = new Response();
        int choice1 = (int) req.get("choice1");
        try {
            XMLDecoder xdc=new XMLDecoder(new FileInputStream(path1));
            List<FictionList> list=(List<FictionList>) xdc.readObject();
            xdc.close();
            List<FictionList> list2=new ArrayList<>();
            for (FictionList f:list){
                if (f.getType()==1){
                    list2.add(f);
                }
            }
            for (FictionList l:list2){
                if (l.getNumber()==choice1) {
                    String o=l.getLocation();
                    FileInputStream fin = null;
                    try {
                        fin = new FileInputStream(o);
                        byte[] b = new byte[fin.available()];
                        int length = 0;
                        length = fin.read(b);
                        resp.setSuccess(true);
                        resp.put("neirong",new String(b, 0, length));
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fin.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            resp.setSuccess(false);
            resp.setMsg(e.getMessage());
        }
        return resp;
    }

    //读言情
    public Response redyanqing(Request req){
        Response resp = new Response();
        int choice1 = (int) req.get("choice1");
        try {
            XMLDecoder xdc=new XMLDecoder(new FileInputStream(path1));
            List<FictionList> list=(List<FictionList>) xdc.readObject();
            xdc.close();
            List<FictionList> list2=new ArrayList<>();
            for (FictionList f:list){
                if (f.getType()==2){
                    list2.add(f);
                }
            }
            for (FictionList l:list2){
                if (l.getNumber()==choice1) {
                    String l1=l.getLocation();
                    FileInputStream fin = null;
                    try {
                        fin = new FileInputStream(l1);
                        byte[] b = new byte[fin.available()];
                        int length = 0;
                        length = fin.read(b);
                        resp.setSuccess(true);
                        resp.put("neirong",new String(b, 0, length));
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fin.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            resp.setSuccess(false);
            resp.setMsg(e.getMessage());
        }
        return resp;
    }


    //下载
    public Response download(Request req) {
        Response resp = new Response();
        //文件名，发文件内容
        String filename =(String)req.get("filename");
        //发送文件
        try {
            FileInputStream fis=new FileInputStream("C:\\Users\\Administrator\\Desktop\\xinjian"+filename);
            byte[]b=new byte[fis.available()];
            int length = 0;
            length = fis.read(b);
            resp.put("content",new String(b, 0, length));
            fis.close();
            resp.setSuccess(true);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Response(false,"文件保存失败");
    }

    //上传
    public Response uploading(Request req) {
        int type=(int)req.get("type");
        String fname =(String)req.get("fname");
        String author=(String)req.get("author");
        String introduce=(String)req.get("introduce");
        String location=(String)req.get("location");
        List<FictionList> list=new ArrayList<>();
        for (FictionList f:list){
            if (f.getType()==type){
                int max=list.indexOf(list.size()-1);
                int number=list.get(max).getNumber()+1;
                list.add(new FictionList(type,number,fname,author,introduce,location));
            }
        }
        XMLEncoder xec= null;
        byte[]b=(byte[])req.get("neirong");
        try {
            xec = new XMLEncoder(new FileOutputStream("D:\\76班java workspace\\Y2Java项目\\JavaSenior\\src\\Txtfiction\\fiction1.xml"));
            xec.writeObject(list);
            FileOutputStream fos=new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\xinjian"+location));
            fos.write(b);
            fos.close();
            Response  r= new Response(true,"文件上传成功");
            return r;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            xec.close();
        }
        return new Response(false,"文件上传失败");
    }

    @Override
    public void run() {
        System.out.println(s + "连接了。。。");
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            ObjectOutputStream oout = new ObjectOutputStream(out);
            ObjectInputStream oin = new ObjectInputStream(in);
            while (true) {
                Request req = (Request) oin.readObject();
                System.out.println(s + "访问:" + req.getAction());
                Response resp = null;
                //反射代替if else
                Method m=this.getClass().getDeclaredMethod(req.getAction(),Request.class);
                resp =(Response) m.invoke(this, req);
                oout.writeObject(resp);
                oout.reset();//!!!!!!!!
                oout.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }
    }
}
